package com.itheima.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.constant.MessageConstant;
import com.itheima.constant.RedisConstant;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.pojo.Address;
import com.itheima.service.AddressService;
import com.itheima.utils.QiniuUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import redis.clients.jedis.JedisPool;

import java.io.IOException;
import java.util.List;
import java.util.UUID;
/*
地址相关
 */
@RestController
@RequestMapping("/map")
public class AddressController {

    //操作redis
    @Autowired
    private JedisPool jedisPool;
    //连接注册中心
    @Reference
    private AddressService addressService;

    //文件上传
    @RequestMapping("/upload")
    public Result upload(@RequestParam("imgFile") MultipartFile imgFile) {
        //TODO 记得做数据校验，校验数据的大小，类型，还有文件名拼接时注意长度

        try {
            //获取原来文件的后缀
            String originalFilename = imgFile.getOriginalFilename();//原始文件名
            //只需要后缀 所以要截取
            //确定最后一个点所在的位置
            int lastIndexOf = originalFilename.lastIndexOf(".");
            String substring = originalFilename.substring(lastIndexOf);//.jsp
// originalFilename.substring(lastIndexOf - 1);会截取到文字 重中之重.jpg  重.jpg

            //使用uuid产生一个名称 加上取出的后缀名
            String fileName = UUID.randomUUID().toString() + substring;

            QiniuUtils.upload2Qiniu(imgFile.getBytes(), fileName);//指定名称 但不能重复

            //将上传的图片名称存入redis,基于redis的set集合存储
            //sadd 就是 操作set集合    调用RedisConstant封装的 总的图片 包含成功与不成功的
            jedisPool.getResource().srem("addressPicResources", fileName);
            //上传成功
            return new Result(true, MessageConstant.PIC_UPLOAD_SUCCESS, fileName);
        } catch (IOException e) {
            e.printStackTrace();
            //上传失败
            return new Result(false, MessageConstant.PIC_UPLOAD_FAIL);
        }
    }


    //页面查询
    @RequestMapping("/findPage")
    public PageResult findPage(@RequestBody QueryPageBean queryPageBean) {
        PageResult pageResult = addressService.pageQuery(
                queryPageBean.getCurrentPage(),
                queryPageBean.getPageSize(),
                queryPageBean.getQueryString());
        return pageResult;
    }


    //添加
    @RequestMapping("/add")
    public Result add(@RequestBody Address address) {
        //因为前段传来的是json格式的数据，需要加入requestbody来封装
        try {
            addressService.add(address);
        } catch (Exception e) {
            e.printStackTrace();
            //服务调用失败
            return new Result(false, "地图新增失败");
        }
        return new Result(true, "地图新增成功");
    }

//删除
    @RequestMapping("/delete")
    public Result delete(Integer id) {
        try {
            addressService.deleteById(id);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, "地图删除失败");
        }
        return new Result(true, "地图删除成功");
    }

//修改
    @RequestMapping("/edit")
    public Result edit(@RequestBody Address address) {
        try {
            addressService.edit(address);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, "地图编辑失败");
        }
        return new Result(true, "地图编辑成功");
    }


    //根据id查询
    @RequestMapping("/findById")
    public Result findById(Integer id) {
        Address address = null;
        try {
            address = addressService.findById(id);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, "查询失败");
        }
        return new Result(true, "查询成功", address);
    }


    //查找全图
    @RequestMapping("/findAllmap")
    public List<String> findAllmap(){
        return addressService.findAllmap();
    }

}